package android.slc.box.commonlibrary.compat;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 * 包括时间格式化等等工具
 */
public class SlcTimeUtils {
    public final static int VALUE_MINUTE_WITH_SECOND = 60;//1分钟/秒单位
    public final static int VALUE_MINUTE_WITH_MILLIS = 60 * 1000;//一分钟/毫秒单位
    public final static int VALUE_HOUR_WITH_MILLIS = VALUE_MINUTE_WITH_MILLIS * 60;
    public final static int VALUE_HOUR_WITH_MINUTE = 60;//1小时/分单位
    public final static int VALUE_DAY_WITH_MILLIS = VALUE_HOUR_WITH_MILLIS * 24;
    /**
     * 清除日历
     *
     * @param calendar
     * @param field
     */
    public static void clearCalendarField(Calendar calendar, int... field) {
        if (calendar != null && field != null) {
            for (int item : field) {
                calendar.set(item, 0);
            }
        }
    }

    public static long clearCalendarField(long timeInMillis, int... field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        clearCalendarField(calendar, field);
        return calendar.getTimeInMillis();
    }

    /*--------------------------日期和LocalDate互转 start-----------------------------*/

    public static LocalDate milliseconds2LocalDate(long milliseconds) {
        Date date = new Date();
        date.setTime(milliseconds);
        return date2LocalDate(date);
    }

    public static LocalDate date2LocalDate(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime milliseconds2LocalDateTime(long milliseconds) {
        Date date = new Date();
        date.setTime(milliseconds);
        return date2LocalDateTime(date);
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date localDate2date(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTime2date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static long localDate2Millisecond(LocalDate localDate) {
        //return localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().getEpochSecond();
        return localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static long localDateTime2Millisecond(LocalDateTime localDateTime) {
        //return localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().getEpochSecond();
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 校验时间是否在指定天数之前
     *
     * @param localDate
     * @param day
     * @return
     */
    public static boolean checkTimeLaterTargetDay(LocalDate localDate, int day) {
        return checkTimeLaterTargetDay(localDate.atStartOfDay(), day);
    }

    /**
     * 校验时间是否在指定天数之前
     *
     * @param localDateTime
     * @param day
     * @return
     */
    public static boolean checkTimeLaterTargetDay(LocalDateTime localDateTime, int day) {
        LocalDateTime currentLocalDateTime = LocalDateTime.now().minusDays(day);
        return currentLocalDateTime.isBefore(localDateTime);
    }

    /*--------------------------日期和LocalDate互转 end-----------------------------*/

    /*--------------------------QDateInfo相关 start-----------------------------*/

    /**
     * 字符串年月转int
     *
     * @param yearMonth
     * @return
     */
    public static QDateInfo strYM2Int(String yearMonth) {
        String year = yearMonth.split("-")[0];
        String month = yearMonth.split("-")[1];
        QDateInfo qDateInfo = new QDateInfo();
        qDateInfo.setYear(Integer.parseInt(year));
        qDateInfo.setMonth(Integer.parseInt(month));
        return qDateInfo;
    }
    public static class QDateInfo implements Serializable {

        protected Integer year;

        protected Integer month;

        protected Integer day;

        public Integer getYear() {
            return year;
        }

        public void setYear(Integer year) {
            this.year = year;
        }

        public Integer getMonth() {
            return month;
        }

        public void setMonth(Integer month) {
            this.month = month;
        }

        public Integer getDay() {
            return day;
        }

        public void setDay(Integer day) {
            this.day = day;
        }
    }

    /*--------------------------QDateInfo相关 end-----------------------------*/

    /**
     * 时间转int数值
     *
     * @param time
     * @return
     */
    public static int[] hourStr2IntArray(String time) {
        String[] timeStrArray = time.split(":");
        int[] intArray = new int[3];
        switch (timeStrArray.length) {
            case 3:
                intArray[2] = Integer.parseInt(timeStrArray[2]);
            case 2:
                intArray[1] = Integer.parseInt(timeStrArray[1]);
            case 1:
                intArray[0] = Integer.parseInt(timeStrArray[0]);
                break;
        }
        return intArray;
    }

    /**
     * 毫秒转小时
     *
     * @param millisecond
     * @return
     */
    public static float millisecond2Hour(long millisecond) {
        return millisecond / (float) VALUE_HOUR_WITH_MILLIS;
    }

    /**
     * 小时转毫秒
     *
     * @param hour
     * @return
     */
    public static long hour2Millisecond(float hour) {
        return (long) (hour * VALUE_HOUR_WITH_MILLIS);
    }

    /**
     * 分钟转小时
     *
     * @param minute
     * @return
     */
    public static float minute2Hour(int minute) {
        return (float) minute / VALUE_HOUR_WITH_MINUTE;
    }
}
